package com.module.product.dao;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;

import base.common.CUDEnum;
import base.dao.GenericHibernateDao;
import base.util.Page;

import com.module.product.vo.ProductGroup;
/**
*
* 商品组合 dao类
*
* @author  王勇云
* @version  [V1.0, Jul 22, 2011]
* @see  [相关类/方法]
* @since  [二代服务平台/商品属性组]
*/
public class ProductGroupDao extends GenericHibernateDao<ProductGroup, String>{

	private static final ProductGroupDao instance = new ProductGroupDao();
	
	private ProductGroupDao(){}
	
	public synchronized static ProductGroupDao getInstance() {
		return instance;
	}
	
	/**
	 * <逻辑删除>
	 * <功能详细描述>
	 * @param ProductGroup@return
	 * @see [类、类#方法、类#成员]
	 */
    public boolean deleteObjs(Session session,ProductGroup obj){
        boolean flag = false;
        try {
            String hql = "update ProductGroup set updaterId = ?,updateTime=?,status =? where id in ("+obj.getId()+")";
            Query query  = session.createQuery(hql); 
                  query.setParameter(0,obj.getUpdaterId());
                  query.setParameter(1, new Date());
                  query.setParameter(2, CUDEnum.D.toString());
                  int i  = query.executeUpdate();
            flag = true;
            return flag;
        } catch (Exception ex) {
            ex.printStackTrace();
            return flag;
        }
    }

	
    public List<ProductGroup> listAll(Session session){
        List<ProductGroup> list  = new ArrayList();
        Query query = session.createQuery("from ProductGroup as obj where obj.status <>? ");
        query.setParameter(0,CUDEnum.D.toString());
        list = query.list();
        return list;
    }

    
    public Page getList(Session session,ProductGroup obj,Page page){
        String hql1 = "from ProductGroup as obj where obj.status !='D' ";
        String hql2 = "select count(*) from ProductGroup as obj  where obj.status !='D' ";
        if(StringUtils.isNotBlank(obj.getName())){
            hql1=hql1 +  " and obj.name = '"+obj.getName()+"'";
            hql2=hql2 +  " and obj.name = '"+obj.getName()+"'";
        }
        Query query = session.createQuery(hql1);
              query.setFirstResult(page.getStart()).setMaxResults(page.getLimit());
        
        page.setTotal((long) ((Long) session.createQuery(hql2)
            .uniqueResult()).intValue());
        page.setRows(query.list());
        return page;
    } 
}
